package helpers;

import interfaces.State;
import java.util.HashSet;
import automaton.DFA;

public class Partitions {
	/**
	 * Initial the first partition in DFA.
	 * @param automata
	 * @return
	 */
	public static HashSet<HandleHashSet> initial(DFA automata){
		
		HashSet<HandleHashSet> result = new HashSet<HandleHashSet>();
		HandleHashSet terminalList = new HandleHashSet();
		HandleHashSet unterminalList = new HandleHashSet();
		char begin ='a';
		terminalList.setLabel(begin);
		begin++;
		unterminalList.setLabel(begin);
		for (State state : automata.getDFA().keySet()) {
			if(state.terminal())
				terminalList.add(state);
			else
				unterminalList.add(state);
		}
		result.add(terminalList);
		result.add(unterminalList);
		return result;
	}
	
	/**
	 * Get the set of the smaller size of sets in the partition
	 * @param partition
	 * @return
	 */
	public static HandleHashSet minimalSet(HashSet<HandleHashSet> partition){
		int minimum = Integer.MAX_VALUE;
		HandleHashSet result = null;
		for (HandleHashSet handleHashSet : partition) {
			if(handleHashSet.size() < minimum){
				minimum = handleHashSet.size();
				result = handleHashSet;
			}
		}
		return result;
	}
	
	/**
	 * intersection between two partitions
	 * @param partition1
	 * @param partition2
	 * @return
	 */
	public static HashSet<HandleHashSet> intersectionPartitions(HashSet<HandleHashSet> partition1, HashSet<HandleHashSet> partition2 ){
		HashSet<HandleHashSet> result = new HashSet<>();
		for (HandleHashSet handleHashSet1 : partition1) {
			for (HandleHashSet handleHashSet2 : partition2) {
				HandleHashSet intersectionResult = HandleHashSet.interSection(handleHashSet1, handleHashSet2); 
				if(intersectionResult.size()>0)
					result.add(intersectionResult);
			}
		}
		return result;
	}
}
